#!/usr/bin/env bash
# Docker Env
START_TIME=$(date +%s)

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8

CUR_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
IMAGE_PATH=${CUR_PATH}/images

source ${CUR_PATH}/.env

# if [[ -z "$(command -v docker)" ]]; then
#     function docker() {
#         if [[ -n "command -v nerdctl" ]]; then
#             nerdctl $@
#         else
#             ${HOME}/.boxs/bin/docker $@
#         fi
#     }
# fi

for denv in $@; do
    [[ "$(echo $denv | grep '\--registry-prefix=')" ]] && DOCKER_REGISTRY_PREFIX=$(echo $denv | awk -F '=' '{print $2}')
    [[ "$(echo $denv | grep '\--registry-prefix=')" ]] && DOCKER_REGISTRY_PREFIX=$(echo $denv | awk -F '=' '{print $2}')
    [[ "$(echo $denv | grep '\--with=github')" ]] && ENABLE_GITHUB_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=coding')" ]] && ENABLE_CODING_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=baidu')" ]] && ENABLE_BAIDU_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=huawei')" ]] && ENABLE_HUAWEI_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=aliyun')" ]] && ENABLE_ALIYUN_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=tencent')" ]] && ENABLE_TENCENT_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=gitlab')" ]] && ENABLE_GITLAB_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=quay')" ]] && ENABLE_QUAY_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=gcr')" ]] && ENABLE_GCR_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=gar')" ]] && ENABLE_GAR_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=azure')" ]] && ENABLE_AZURE_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=ucloud')" ]] && ENABLE_UCLOUD_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=amazon')" ]] && ENABLE_AMAZON_REGISTRY=true
    [[ "$(echo $denv | grep '\--with=ecr')" ]] && ENABLE_ECR_REGISTRY=true
    [[ "$(echo $denv | grep '\--prefix')" ]] && DOCKER_REGISTRY_PREFIX=$(echo $denv | awk -F '=' '{print $2}')
done

_red() {
    printf '\033[1;31;31m%b\033[0m' "$1"
}

_green() {
    printf '\033[1;31;32m%b\033[0m' "$1"
}

_yellow() {
    printf '\033[1;31;33m%b\033[0m' "$1"
}

_info() {
    _green "[Info] "
    printf -- "%s" "$1"
    printf "\n"
}

_warn() {
    _yellow "[Warning] "
    printf -- "%s" "$1"
    printf "\n"
}

_error() {
    _red "[Error] "
    printf -- "%s" "$1"
    printf "\n"
    exit 1
}

# Build Images
do_build() {
    NAME=$2
    VERSION=$3
    cd "${IMAGE_PATH}" || exit
    if [[ -d "$NAME" ]]; then
        if [[ -z "${VERSION}" ]]; then
            VERSION='latest'
        fi
        cd "${NAME}/${VERSION}" || exit
        DOCKER_BUILDKIT=0

        # --no-cache
        if [[ -n "${DOCKER_REGISTRY_PREFIX}" ]]; then
            # docker build --progress plain --no-cache -t "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" .
            # docker build --no-cache -t "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" .
            docker build --no-cache --progress plain -t "${DOCKER_REGISTRY_PREFIX}/${NAME}:"${VERSION}"" .
        else
            # docker build --progress plain --no-cache -t ${NAME}:"${VERSION}" .
            docker build --no-cache --progress plain -t "${NAME}:"${VERSION}"" .
        fi

        [[ -n "${ENABLE_GITHUB_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_GITHUB}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_CODING_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_CODING}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_BAIDU_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_BAIDU}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_HUAWEI_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_HK_HUAWEI}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_ALIYUN_SH_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_ALIYUN_SH}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_ALIYUN_HK_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_ALIYUN_HK}/${NAME}:"${VERSION}"

        [[ -n "${ENABLE_TENCENT_GZ_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_TENCENT_GZ}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_TENCENT_HK_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_TENCENT_HK}/${NAME}:"${VERSION}"

        [[ -n "${ENABLE_GITLAB_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_GITLAB}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_QUAY_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_QUAY}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_GCR_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_GCR}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_GAR_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_GAR}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_AZURE_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" "${DOCKER_REGISTRY_AZURE}"/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_UCLOUD_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_UCLOUD}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_AMAZON_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_AMAZON}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_ECR_REGISTRY}" ]] && docker tag "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ${DOCKER_REGISTRY_RCR}/${NAME}:"${VERSION}"
    fi
}

# 批量构建
do_builds() {
    NAME=$2
    VERSION=$3
    cd ${IMAGE_PATH} || exit 1
    if [[ -d "$NAME" ]]; then
        cd ${NAME} || exit 1
        for ver in $(ls); do
            VERSION="${ver}"
            if [[ -f "${IMAGE_PATH}/${NAME}/${VERSION}/Dockerfile" ]]; then
                cd "${IMAGE_PATH}/${NAME}/${VERSION}" || exit 1
                do_build build ${NAME} ${VERSION}
                _info "${NAME}:${VERSION} Build Complete"
                # CMD="docker build --no-cache -t "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}" ."
            fi
        done
    fi
}

# Push Images to Registry
do_push() {
    NAME=$2
    VERSION=$3
    cd "${IMAGE_PATH}" || exxit 1
    if [[ -d "$NAME" ]]; then
        cd "${NAME}/${VERSION}" || exit 1
        if [[ -z "${VERSION}" ]]; then
            VERSION='latest'
        fi

        [[ -n "${ENABLE_GITHUB_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_GITHUB}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_CODING_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_CODING}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_BAIDU_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_BAIDU}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_HUAWEI_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_HUAWEI}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_ALIYUN_SH_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_ALIYUN_SH}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_ALIYUN_HK_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_ALIYUN_HK}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_TENCENT_GZ_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_TENCENT_GZ}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_TENCENT_HK_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_TENCENT_HK}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_GITLAB_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_GITLAB}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_QUAY_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_QUAY}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_GCR_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_GCR}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_GAR_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_GAR}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_AZURE_REGISTRY}" ]] && docker push "${DOCKER_REGISTRY_AZURE}"/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_UCLOUD_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_UCLOUD}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_AMAZON_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_AMAZON}/${NAME}:"${VERSION}"
        [[ -n "${ENABLE_ECR_REGISTRY}" ]] && docker push ${DOCKER_REGISTRY_RCR}/${NAME}:"${VERSION}"

        if [[ -n "${DOCKER_REGISTRY_PREFIX}" ]]; then
            docker push "${DOCKER_REGISTRY_PREFIX}"/${NAME}:"${VERSION}"
        else
            docker push ${NAME}:"${VERSION}"
        fi

    fi
}

# 批量上传
do_pushs() {
    NAME=$2
    VERSION=$3
    cd ${IMAGE_PATH}
    if [[ -d "$NAME" ]]; then
        cd ${NAME}
        for ver in $(ls); do
            VERSION="${ver}"
            if [[ -f "${IMAGE_PATH}/${NAME}/${VERSION}/Dockerfile" ]]; then
                cd "${IMAGE_PATH}/${NAME}/${VERSION}"
                do_push push ${NAME} ${VERSION}
                _info "${NAME}:${VERSION} Push Complete"
            fi
        done
    fi
}

do_pull() {
    NAME=$2
    VERSION=$3
    if [[ -z "$NAME" ]]; then
        echo -e "pull requires exactly 1 argument See \n'docker pull --help'."
    fi
    if [[ -z ""${VERSION}"" ]]; then
        VERSION="latest"
    fi
    CMD="docker pull "${DOCKER_REGISTRY_PREFIX}"/${NAME}:${VERSION}"
    ${CMD}
}

do_run() {
    echo ''
}

do_login() {
    case $2 in
    'docker')
        # docker login -u ${DOCKER_REGISTRY_BAIDU_USERNAME} -p ${DOCKER_REGISTRY_BAIDU_PASSWORD} registry.baidubce.com
        echo ${DOCKER_REGISTRY_PASSWORD} | docker login -u ${DOCKER_REGISTRY_USERNAME} --password-stdin
        ;;
    'baidu')
        # docker login -u ${DOCKER_REGISTRY_BAIDU_USERNAME} -p ${DOCKER_REGISTRY_BAIDU_PASSWORD} registry.baidubce.com
        echo ${DOCKER_REGISTRY_BAIDU_PASSWORD} | docker login -u ${DOCKER_REGISTRY_BAIDU_USERNAME} --password-stdin registry.baidubce.com
        ;;
    'coding')
        docker login -u ${DOCKER_REGISTRY_CODING_USERNAME} -p ${DOCKER_REGISTRY_CODING_PASSWORD} pkgs-docker.pkg.coding.net
        ;;
    # 阿里云杭州
    'aliyun-hz')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.cn-hangzhou.aliyuncs.com
        ;;
    # 阿里云新加坡
    'aliyun-sg')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.ap-southeast-1.aliyuncs.com
        ;;
    # 阿里云硅谷 silicon valley
    'aliyun-sv')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.us-west-1.aliyuncs.com
        ;;
    # 阿里云迪拜 Dubai
    'aliyun-db')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.me-east-1.aliyuncs.com
        ;;
    # 阿里云韩国
    'aliyun-kr')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.ap-northeast-2.aliyuncs.com
        ;;
    # 阿里云上海
    'aliyun-sh')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.cn-shanghai.aliyuncs.com
        ;;
    # 阿里云 Japan
    'aliyun-jp')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.ap-northeast-1.aliyuncs.com
        ;;
    # 阿里云香港
    'aliyun-hk')
        docker login -u ${DOCKER_REGISTRY_ALIYUN_USERNAME} -p ${DOCKER_REGISTRY_ALIYUN_PASSWORD} registry.cn-hongkong.aliyuncs.com
        ;;
    # 默认广州节点
    'tencent')
        docker login -u ${DOCKER_REGISTRY_TENCENT_USERNAME} -p ${DOCKER_REGISTRY_TENCENT_PASSWORD} ccr.ccs.tencentyun.com
        ;;
    'tencent-sg')
        docker login -u ${DOCKER_REGISTRY_TENCENT_USERNAME} -p ${DOCKER_REGISTRY_TENCENT_PASSWORD} sgccr.ccs.tencentyun.com
        ;;
    'tencent-hk')
        docker login -u ${DOCKER_REGISTRY_TENCENT_USERNAME} -p ${DOCKER_REGISTRY_TENCENT_PASSWORD} hkccr.ccs.tencentyun.com
        ;;
    'tencent-kr')
        docker login -u ${DOCKER_REGISTRY_TENCENT_USERNAME} -p ${DOCKER_REGISTRY_TENCENT_PASSWORD} krccr.ccs.tencentyun.com
        ;;
    'tencent-jp')
        docker login -u ${DOCKER_REGISTRY_TENCENT_USERNAME} -p ${DOCKER_REGISTRY_TENCENT_PASSWORD} jpccr.ccs.tencentyun.com
        ;;
    'huawei-hk')
        docker login -u ${DOCKER_REGISTRY_HUAWEI_USERNAME} -p ${DOCKER_REGISTRY_HUAWEI_PASSWORD} swr.ap-southeast-1.myhuaweicloud.com
        ;;
    # 美西硅谷
    'tencent-usw')
        docker login -u ${DOCKER_REGISTRY_TENCENT_USERNAME} -p ${DOCKER_REGISTRY_TENCENT_PASSWORD} uswccr.ccs.tencentyun.com
        ;;
    *)
        echo 'Unknow Platform'
        ;;
    esac
}

_run_compose() {
    if [[ -f "${CUR_PATH}/docker-compose.yml" ]]; then
        docker-compose up $@
    fi
}

_stop_compose() {
    if [[ -f "${CUR_PATH}/docker-compose.yml" ]]; then
        docker-compose stop $@
    fi
}

_rm_compose() {
    if [[ -f "${CUR_PATH}/docker-compose.yml" ]]; then
        docker-compose stop $@
        docker-compose rm $@
    fi
}

usage() {
    echo "	Docker Env Build Tool
/-----------------------------------\\
|   build image name tag
|   push  image name tag
|   pull  image name tag
|   login login to registry ,avaiable platform : baidu coding aliyun-hk tencent tencent-hk tencent-sg tencent-kr tencent-jp tencent-usw (silicon valley)
|
|Params:
|   --registry-prefix       set docker prefix, example : docker.com/user
\\-----------------------------------/"
}

case $1 in
build)
    do_build $*
    ;;
builds)
    do_builds $*
    ;;
compose)
    arg=$@
    arg=$(echo $arg | sed "s#${1}##g")
    arg=$(echo $arg | sed "s#${2}##g")
    case "$2" in
    run)
        _run_compose $arg
        ;;
    stop)
        _stop_compose $arg
        ;;
    rm)
        _rm_compose $arg
        ;;
    *)
        echo "Unknow Options $2"
        exit 1
        ;;
    esac

    ;;
run)
    do_run $*
    ;;
push)
    do_push $*
    ;;
pushs)
    do_pushs $*
    ;;
pull)
    do_pull $*
    ;;
login)
    do_login $*
    ;;
*)
    usage
    ;;
esac

END_TIME=$(date +%s)
FULL_EXEC_TIME=$(expr ${END_TIME} - ${START_TIME})
_info "==> Total Time: ${FULL_EXEC_TIME} Second "
